<?php

namespace Database\Seeders;

use App\Models\GoldBean;
use App\Models\GoldCoin;
use App\Models\GoldSeed;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\GoldProduct;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
use App\Services\RefreshGoldProductTakeState\RefreshGoldProductTakeState;

class GoldStormBackOneDay extends Seeder
{
	protected function backUsers()
	{
		GoldCharge::all()->each(function ($item) {
			$item->update([
				'created_at' => $item->created_at->addDays(-1),
				'updated_at' => $item->updated_at->addDays(-1),
			]);
		});
		
		GoldCoin::all()->each(function ($item) {
			$item->update([
				'created_at' => $item->created_at->addDays(-1),
				'updated_at' => $item->updated_at->addDays(-1),
			]);
		});
		
		GoldBean::all()->each(function ($item) {
			$item->update([
				'created_at' => $item->created_at->addDays(-1),
				'updated_at' => $item->updated_at->addDays(-1),
			]);
		});
	}
	
	protected function backGoldRecords()
	{
		GoldRecord::query()->chunks(20, function (Collection $records) {
			
			if ($records->isEmpty()) return false;
			
			/**
			 * @var GoldRecord $record
			 */
			foreach ($records as $record) {
				$user = $record->user;
				$cacheId = $record->cacheKey();
				
				$isInPendingCache = $user->isRecordInPendingCache($cacheId);
				$isInRunningCache = $user->isRecordInRunningCache($cacheId);
				$isInFinishedCache = $user->isRecordInFinishedCache($cacheId);
				
				$user->delRecordFromPendingCache($cacheId);
				$user->delRecordFromRunningCache($cacheId);
				$user->delRecordFromFinishedCache($cacheId);
				
				$user->delMyRecordCache($cacheId);
				
				$record->update([
					'paid_at' => $record->paid_at ? $record->paid_at->addDays(-1) : null,
					'to_resell_applied_at' => $record->to_resell_applied_at ? $record->to_resell_applied_at->addDays(-1) : null,
					'created_at' => $record->created_at->addDays(-1),
					'updated_at' => $record->updated_at->addDays(-1),
				]);
				
				$record = $record->fresh();
				
				if ($isInPendingCache) {
					$user->syncRecordToPendingCache($record);
				}
				if ($isInRunningCache) {
					$user->syncRecordToRunningCache($record);
				}
				if ($isInFinishedCache) {
					$user->syncRecordToFinishedCache($record);
				}
			}
			
			return true;
		});
	}
	
	protected function backGoldProducts()
	{
		GoldProduct::query()->chunks(20, function (Collection $products) {
			
			if ($products->isEmpty()) return false;
			
			/**
			 * @var GoldProduct $product
			 */
			foreach ($products as $product) {
				$product->update([
					'is_taken_today' => false,
					'last_taken_at' => $product->last_taken_at ? $product->last_taken_at->addDays(-1) : null,
					'last_resold_at' => $product->last_resold_at ? $product->last_resold_at->addDays(-1) : null,
					'self_taken_at' => $product->self_taken_at ? $product->self_taken_at->addDays(-1) : null,
					'self_split_at' => $product->self_split_at ? $product->self_split_at->addDays(-1) : null,
					'split_from_record_at' => $product->split_from_record_at ? $product->split_from_record_at->addDays(-1) : null,
					'created_at' => $product->created_at->addDays(-1),
					'updated_at' => $product->updated_at->addDays(-1),
				]);
			}
			
			return true;
		});
	}
	
	protected function backGoldSeeds()
	{
		GoldSeed::query()->chunks(20, function (Collection $seeds) {
			
			if ($seeds->isEmpty()) return false;
			
			/**
			 * @var GoldSeed $seed
			 */
			foreach ($seeds as $seed) {
				$seed->update([
					'created_at' => $seed->created_at->addDays(-1),
					'updated_at' => $seed->updated_at->addDays(-1),
				]);
			}
			
			return true;
		});
	}
	
    public function run()
    {
    	DB::transaction(function () {
		    $this->backUsers();
		    $this->backGoldRecords();
		    $this->backGoldProducts();
		    $this->backGoldSeeds();
	    });
    	
	    app(RefreshGoldProductTakeState::class)();
    }
}
